import { useAppDispatch, useAppSelector } from "@/store/hooks";
import {
  getChannelAsync,
  changeParams,
  changeCreate,
} from "@/store/withdrawChannel/withdrawChannelSlice";
import { Button, Card, Select, Space } from "antd";
import { isEqual } from "lodash-es";
import type { ReactElement } from "react";
import { shallowEqual } from "react-redux";

export default function Filter(): ReactElement<typeof Card, "div"> {
  const {
    config: {
      data: { data: { user_channel_list = [], wd_way_conf = [] } = {} } = {},
    } = {},
  } = useAppSelector((state) => state.app, shallowEqual);
  const { params, table } = useAppSelector(
    (state) => state.withdrawChannel,
    shallowEqual
  );
  const dispatch = useAppDispatch();
  return (
    <Card>
      <Space
        wrap
        size="large"
        className="w-full flex justify-between"
        classNames={{ item: "last:ml-auto" }}
      >
        <label>
          <span>渠道：</span>
          <Select
            value={params.channel}
            options={[{ label: "全部", value: "" }, ...user_channel_list]}
            className="min-w-[120px]"
          />
        </label>
        <label>
          <span>提现平台：</span>
          <Select
            value={params.wd_way}
            options={[{ label: "全部", value: "" }, ...wd_way_conf]}
            className="min-w-[120px]"
          />
        </label>
        <Button.Group>
          <Button type="primary" ghost onClick={() => {
            dispatch(changeCreate({
              type: 'create',
              open: true
            }))
          }}>
            创建
          </Button>
          <Button
            type="primary"
            loading={table.status === "loading"}
            onClick={(): void => {
              if (isEqual(params, table?.params)) {
                dispatch(getChannelAsync({...params}));
              } else {
                const newParams = {
                  ...params,
                  page: 1,
                };
                dispatch(changeParams(newParams));
                dispatch(getChannelAsync(newParams));
              }
            }}
          >
            查询
          </Button>
        </Button.Group>
      </Space>
    </Card>
  );
}
